<?php

class MenuController extends AdminController {
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    //public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionIndex() {
        $model = site_menu::listData();
        $this->render('admin', array(
            'data' => $model,
        ));
    }

    public function actionOrder_by() {

        //explode POST
        $var_order = explode("_", $_POST['id']);

        if ($var_order['0'] == 'up') {
            $condition = '<=';
            $sort = 'DESC';
        } elseif ($var_order['0'] == 'down') {
            $condition = '>=';
            $sort = 'ASC';
        }

        //$par_id = $db->GetOne("SELECT menu_par_id FROM site_menu WHERE menu_id = '" . $id . "'");
        $par_id = Yii::app()->db->createCommand()
                ->select('menu_par_id')
                ->from('site_menu')
                ->where('menu_id=:id', array(':id' => $var_order['1']))
                ->order('menu_order_by DESC')
                ->queryScalar();

        //$order_by = $db->GetOne("SELECT menu_order_by FROM site_menu WHERE menu_id = '" . $id . "'");
        $order_by = Yii::app()->db->createCommand()
                ->select('menu_order_by')
                ->from('site_menu')
                ->where('menu_id=:id', array(':id' => $var_order['1']))
                ->queryScalar();


        $sql_menu = "SELECT menu_id, menu_order_by
                  FROM site_menu
                  WHERE menu_order_by " . $condition . " " . $order_by . "
                  AND menu_id != '" . $var_order['1'] . "'
                  AND menu_par_id = '" . $par_id . "'
                  ORDER BY menu_order_by " . $sort . "
                  LIMIT 1";

        $row_menu = Yii::app()->db->createCommand($sql_menu)->queryRow();
        echo "UPDATE site_menu SET menu_order_by = '" . $order_by . "' WHERE menu_id = '" . $row_menu['menu_id'] . "'";
        echo "<br>" . "UPDATE site_menu SET menu_order_by = '" . $row_menu['menu_order_by'] . "' WHERE menu_id = '" . $var_order['1'] . "'";
        Yii::app()->db->createCommand("UPDATE site_menu SET menu_order_by = '" . $order_by . "' WHERE menu_id = '" . $row_menu['menu_id'] . "'")->execute();
        Yii::app()->db->createCommand("UPDATE site_menu SET menu_order_by = '" . $row_menu['menu_order_by'] . "' WHERE menu_id = '" . $var_order['1'] . "'")->execute();
    }

    public function actionCreate() {
        $model = new site_menu;

        if (isset($_POST['site_menu'])) {

            $order_by = Yii::app()->db->createCommand()
                    ->select('menu_order_by')
                    ->from('site_menu')
                    ->order('menu_order_by DESC')
                    ->queryScalar();

            $model->attributes = $_POST['site_menu'];

            $model->menu_is_active = "1";
            $model->menu_order_by = $order_by + 1;


            if ($model->save()) {

                $this->redirect(array('index'));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        if (isset($_POST['site_menu'])) {
            $menu_par_id = Yii::app()->db->createCommand()
                    ->select('menu_par_id')
                    ->from('site_menu')
                    ->where('menu_id=:id', array(':id' => $model->menu_id))
                    ->queryScalar();

            $model->attributes = $_POST['site_menu'];

            $model->menu_location = 'user';

            if ($model->menu_par_id != $menu_par_id) {
                $order_by = Yii::app()->db->createCommand()
                        ->select('menu_order_by')
                        ->from('site_menu')
                        ->where('menu_par_id=:par_id AND menu_id!=:id', array(':par_id' => $model->menu_par_id, ':id' => $model->menu_id))
                        ->order('menu_order_by DESC')
                        ->queryScalar();

                $model->menu_order_by = $order_by + 1;
            }

            if ($model->save()) {
                $this->redirect(array('index'));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            try {
                $this->loadModel($id)->delete();
                echo '1';
            } catch (Exception $e) {
                echo '0';
            }
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function loadModel($id) {
        $model = site_menu::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'site-news-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}

?>
